package com.chuyi.demo.frame;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.chuyi.demo.util.WindowUtilities;

public class JListSimpleExample extends JFrame{

	public JListSimpleExample(){
		super("Creating a Simple List");
		WindowUtilities.setNativeLookAndFeel();
		Container container = getContentPane();
		
		String[] entries = { "Entry 1", "Entry 2", "Entry 3",
                "Entry 4", "Entry 5", "Entry 6" };
		final JList sampleJList = new JList(entries);
		sampleJList.setVisibleRowCount(4);
		Font displayFont = new Font("Serif", Font.BOLD, 18);
	    sampleJList.setFont(displayFont);
		
	    JScrollPane listPane = new JScrollPane(sampleJList);
	    
	    JPanel listPanel = new JPanel();
	    listPanel.setBackground(Color.white);
	    Border listPanelBorder = BorderFactory.createTitledBorder("Sample JList");
	    listPanel.setBorder(listPanelBorder);
	    listPanel.add(listPane);
	    container.add(listPanel, BorderLayout.CENTER);
	    
	    
	    JLabel valueLabel = new JLabel("Last Selection:");
	    valueLabel.setFont(displayFont);
	    final JTextField  valueField = new JTextField("None", 7);
	    valueField.setFont(displayFont);
	    JPanel valuePanel = new JPanel();
	    valuePanel.setBackground(Color.white);
	    Border valuePanelBorder = BorderFactory.createTitledBorder("JList Selection");
	    valuePanel.setBorder(valuePanelBorder);
	    valuePanel.add(valueLabel);
	    valuePanel.add(valueField);
	    container.add(valuePanel, BorderLayout.SOUTH);
	    
	    
	    JButton  button = new JButton();
	    button.setText("点我");
	    container.add(button,BorderLayout.EAST);
	    
	    sampleJList.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				System.out.println(e.getFirstIndex());;
				if(!e.getValueIsAdjusting()){
					valueField.setText(sampleJList.getSelectedValue().toString());
				}
			}
		});
	    
	    button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Object [] objs = sampleJList.getSelectedValues();
				System.out.println(objs.length);;
			}
		});
	    
	    
		pack();
		setVisible(true);
		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	
	public static void main(String[] args) {
		
		
		
		new JListSimpleExample();
		
	}
}
